import { Select } from 'antd';
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { getLocale, setLocale } from 'umi';

const { Option } = Select;

const LanguageSwitcher: React.FC = () => {
  const [currentLocale, setCurrentLocale] = useState<string>(getLocale());

  useEffect(() => {
    const savedLocale = localStorage.getItem('locale');
    if (savedLocale && savedLocale !== currentLocale) {
      setCurrentLocale(savedLocale);
      setLocale(savedLocale);
      axios.defaults.headers.common['Accept-Language'] = savedLocale;
    }
  }, []);

  const handleChange = (value: string) => {
    setCurrentLocale(value);
    setLocale(value);
    axios.defaults.headers.common['Accept-Language'] = value;
    localStorage.setItem('locale', value);
  };

  return (
    <Select value={currentLocale} style={{ width: 120 }} onChange={handleChange}>
      <Option value="zh-CN">中文</Option>
      <Option value="en-US">English</Option>
      {/* 你可以在这里添加更多语言选项 */}
    </Select>
  );
};

export default LanguageSwitcher;
